package cn.com.hhrcw.job;

import cn.com.hhrcw.entity.CvResume;
import cn.com.hhrcw.entity.CvResumeStatistics;
import cn.com.hhrcw.service.ICvResumeService;
import cn.com.hhrcw.service.ICvResumeStatisticsService;
import cn.com.hhrcw.utils.RedisLockUtils;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonConstant;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @program: stdss-boot-parent
 * @description: 统计每天简历量
 * @author: JD
 * @create: 2020-07-28 16:09
 **/
@Component("AdminResumeStatisticsTask")
@Slf4j
public class AdminResumeStatisticsTask implements Job {

    @Autowired
    private ICvResumeService resumeService;

    @Autowired
    private ICvResumeStatisticsService statisticsService;

    @Override
    public void execute(JobExecutionContext context) {
        String key1 = "JOB_RESUME__TASK";
        try {
            if (RedisLockUtils.tryLock(key1, 1, TimeUnit.MINUTES)) {
                log.debug("---------统计总简历与今日新增简历量开始---------");
                Date date = DateUtil.date();
                int total = resumeService.lambdaQuery().le(CvResume::getCreateTime, DateUtil.endOfDay(date)).count();
                //今天新建立量
                int countThisDay = resumeService.lambdaQuery().between(CvResume::getCreateTime, DateUtil.beginOfDay(date), DateUtil.endOfDay(date)).count();
                //创建或更新
                CvResumeStatistics statistics = new CvResumeStatistics();
                statistics.setCreateBy("job");
                statistics.setId(DateUtil.format(date, "yyyyMMdd"));
                statistics.setTotal(total);
                statistics.setTotalThisDay(countThisDay);
                statisticsService.saveOrUpdate(statistics);
                log.debug("---------统计总简历与今日新增简历量结束---------");
            }
        } catch (Exception e) {

        }finally {
            RedisLockUtils.unLock(key1);
        }

    }

}
